package com.inspur.cloud.service.dataspace;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.UserGroupInformation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.IOException;
@Service
public class KerberosLogin {
    private static Logger log = LoggerFactory.getLogger(KerberosLogin.class);

    public void KerberosAuth(String krbConf, Configuration conf,String principal_name,String keypath){
        try{
            System.clearProperty("java.security.krb5.conf");
            System.setProperty("java.security.krb5.conf", krbConf);
            //将HDFS配置文件加载进入kerberos中
            UserGroupInformation.setConfiguration(conf);
            try {
                sun.security.krb5.Config.refresh();
            } catch (Exception e) {
                log.error("刷新kerberos config信息报错：", e);
            }
            //kerberos登录
            UserGroupInformation.loginUserFromKeytab(principal_name, keypath);
        }
        catch (IOException e){
            log.error("kerberos认证失败");
        }
    }
}
